{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "aList = [3, 4, 5, 6, 7, 9, 11, 13, 15, 17]\n",
    "################### 取值操作 ##################\n",
    "## 返回包含原列表中所有的新列表\n",
    "print(aList[::])\n",
    "## 返回包含原列表中所有元素的逆序列表\n",
    "print(aList[::-1])\n",
    "## 隔一个取一个，获取偶数位置的元素\n",
    "print(aList[::2])\n",
    "## 隔一个取一个，获取奇数位置的元素\n",
    "print(aList[1::2])\n",
    "## 指定切片的开始和结束位置\n",
    "print(aList[3:6])\n",
    "## 切片结束位置大于列表长度时，从列表尾部截断\n",
    "aList[0:100]\n",
    "## 切片开始位置大于列表长度时，返回空列表\n",
    "aList[100:]\n",
    "\n",
    "################### 赋值操作 ##################\n",
    "## 在列表尾部增加元素\n",
    "aList[len(aList):] = [9]\n",
    "## 在列表头部插入元素\n",
    "aList[:0] = [1, 2]\n",
    "## 在列表中间位置插入元素\n",
    "aList[3:3] = [4]\n",
    "## 替换列表元素，等号两边的列表长度相等\n",
    "aList[:3] = [1, 2]\n",
    "## 等号两边的列表长度也可以不相等\n",
    "aList[3:] = [4, 5, 6]\n",
    "## 隔一个修改一个\n",
    "aList[::2] = [0] * 3\n",
    "print(aList)\n",
    "\n",
    "## 隔一个修改一个\n",
    "aList[::2] = ['a', 'b', 'c']\n",
    "## 左侧切片不连续，等号两边列表长度必须相等\n",
    "aList[::2] = [1, 2]\n",
    "## 删除列表中前3个元素\n",
    "aList[:3] = []\n",
    "\n",
    "################### 删除元素操作 ##################\n",
    "## 切片元素连续删除\n",
    "del aList[:3]\n",
    "## 切片元素不连续，隔一个删一个\n",
    "del aList[::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['bobby3', 'bobby2', 'imooc', 'bobby1']\n"
     ]
    }
   ],
   "source": [
    "## 支持切片操作\n",
    "\n",
    "import numbers\n",
    "class Group:\n",
    "    def __init__(self, group_name, company_name, staffs):\n",
    "        self.group_name = group_name\n",
    "        self.company_name = company_name\n",
    "        self.staffs = staffs\n",
    "    \n",
    "    def __reversed__(self):\n",
    "        self.staffs.reverse()\n",
    "        return self.staffs\n",
    "    \n",
    "    def __getitem__(self, item):\n",
    "#         return self.staffs[item]\n",
    "        cls = type(self)\n",
    "        if isinstance(item, slice):\n",
    "            return cls(group_name=self.group_name, company_name=self.company_name, staffs=self.staffs[item])\n",
    "        elif isinstance(item, numbers.Integral):\n",
    "            return cls(group_name=self.group_name, company_name=self.company_name, staffs=[self.staffs[item]])\n",
    "        \n",
    "    def __len__(self):\n",
    "        return len(self.staffs)\n",
    "    \n",
    "    def __iter__(self):\n",
    "        return iter(self.staffs)\n",
    "    \n",
    "    def __contains__(self, item):\n",
    "        if item in self.staffs:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n",
    "staffs = ['bobby1', 'imooc', 'bobby2', 'bobby3']\n",
    "group = Group(company_name='imooc', group_name='user', staffs=staffs)\n",
    "\n",
    "sub_group = group[:2]\n",
    "# print(sub_group)\n",
    "# print(group[0])\n",
    "# print(len(group))\n",
    "# if 'bobby1' in group:\n",
    "#     print('yes')\n",
    "\n",
    "## 序列反转\n",
    "print(reversed(group))\n",
    "# for user in group:\n",
    "#     print(user)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
